<?php
/**
 * Created by PhpStorm
 * User: lcz
 * Date: 2019/9/10
 * Time: 22:28
 * 收货地址
 */
namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

class Address extends Api {
	
	protected $noNeedLogin = ['import'];
	/**
	 * 列表
	 */
	public function index(){
		$userId = $this->auth->id;
		
		$rows = Db::name('address')->alias('a')
			->join('area p', 'a.province_id = p.id', 'left')
			->join('area c', 'a.city_id = c.id', 'left')
			->join('area aa', 'a.area_id = aa.id', 'left')
			->where(['a.user_id' => $userId, 'a.status' => \app\common\model\Address::STATUS_NORMAL])
			->field('a.id, a.name, a.mobile, a.province_id, a.city_id, a.area_id, a.address, a.is_default, p.name as province, c.name as city, aa.name as area')
			->order(['a.is_default' => 'desc', 'a.update_time' => 'desc'])
			->select();
		
		$this->success('查询成功', $rows);
	}
	
	/**
	 * 添加、编辑
	 */
	public function edit() {
		$data = $this->request->post();
		$check = new \app\common\validate\Address();
		if(!$check->check($data)) {
			$this->error($check->getError());
		}
		
		$model = new \app\common\model\Address();
		$userId = $this->auth->id;
		$data['user_id'] = $userId;
		
		if($data['is_default'] == 1) {
			//这条为默认，把原来的更改为不是默认
			Db::name('address')
				->where(['user_id' => $userId])
				->update(['is_default' => 0]);
		}
		
		if($data['id']) {
			$res = $model->allowField(true)
				->save($data, ['id' => $data['id'], 'user_id' => $userId]);
		} else {
			$res = $model->isUpdate(false)
				->allowField(true)
				->save($data);
		}
		
		if($res !== false) {
			$this->success('保存成功');
		} else {
			$this->error('保存出错', $model->getError());
		}
	}
	
	/**
	 * 删除
	 */
	public function del(){
		$id = $this->request->post('id');
		!$id && $this->error('参数错误');
		
		$model = new \app\common\model\Address();
		$res = $model
			->save(['status' => $model::STATUS_DELETE],
				['id' => $id, 'user_id' => $this->auth->id]);
		
		if($res === false) {
			$this->error('删除出错');
		} else {
			$this->success('删除成功');
		}
	}
	
	/**
	 * 获取指定地址
	 */
	public function detail() {
		$id = $this->request->post('id', '', 'intval');
		$userId = $this->auth->id;
		
		$where['a.user_id'] = $userId;
		$where['a.status'] = \app\common\model\Address::STATUS_NORMAL;
		
		$id && $where['a.id'] = $id;
		
		$rows = Db::name('address')->alias('a')
			->join('area p', 'a.province_id = p.id', 'left')
			->join('area c', 'a.city_id = c.id', 'left')
			->join('area aa', 'a.area_id = aa.id', 'left')
			->where($where)
			->field('a.id, a.name, a.mobile, a.province_id, a.city_id, a.area_id, a.address, a.is_default, p.name as province, c.name as city, aa.name as area')
			->order(['a.is_default' => 'desc', 'a.update_time' => 'desc'])
			->find();
		
		$this->success('查询成功', $rows);}
	
	public function import() {
		$data = $this->request->post();
		
		$province = $data[0];
		$city = $data[1];
		$area = $data[2];
		
		$provinceRow = Db::name('region')->find($province['id']);
		if(!$provinceRow) {
			$provinceRes = Db::name('region')
				->insert([
					'id' => $province['id'],
					'name' => $province['name'],
					'parentid' => 0,
					'postcode' => '',
					'level' => 1,
					'position' => '',
				]);
			if(!$provinceRes) {
				$this->error('添加省份出错');
			}
		}
		
		$cityRow = Db::name('region')->find($city['id']);
		if(!$cityRow) {
			$cityRes = Db::name('region')
				->insert([
					'id' => $city['id'],
					'name' => $city['name'],
					'parentid' => $province['id'],
					'postcode' => '',
					'level' => 2,
					'position' => $province['id'],
				]);
			if(!$cityRes) {
				$this->error('添加城市出错');
			}
		}
		
		$areaRow = Db::name('region')->find($area['id']);
		if(!$areaRow) {
			$areaRes = Db::name('region')
				->insert([
					'id' => $area['id'],
					'name' => $area['name'],
					'parentid' => $city['id'],
					'postcode' => $area['postcode'],
					'level' => 3,
					'position' => $province['id'].','.$city['id'],
				]);
			if(!$areaRes) {
				$this->error('添加区域出错');
			}
		}
		
		$this->success('添加成功');
	}
}
